package cn.zkrt.aims;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class CreateRtspPullUrl {

    public static void main(String[] args) {
        String camerasJsonStr = "二阶段2球机\trtsp://admin:A12345678@192.168.2.3:554/1/1\n" +
                "厂房消毒间入口门...\trtsp://admin:A12345678@192.168.2.3:554/2/1\n" +
                "一阶段设备区枪机\trtsp://admin:A12345678@192.168.2.3:554/3/1\n" +
                "厂房前门外枪机\trtsp://admin:A12345678@192.168.2.3:554/4/1\n" +
                "厂房后门外枪机\trtsp://admin:A12345678@192.168.2.3:554/5/1\n" +
                "监控室内枪机\trtsp://admin:A12345678@192.168.2.3:554/6/1\n" +
                "二阶段3走道枪机\trtsp://admin:A12345678@192.168.2.3:554/7/1\n" +
                "厂房右后门枪机\trtsp://admin:A12345678@192.168.2.3:554/8/1\n" +
                "二阶段1走道枪机\trtsp://admin:A12345678@192.168.2.3:554/9/1\n" +
                "二阶段3球机\trtsp://admin:A12345678@192.168.2.3:554/10/1\n" +
                "厂房前中门枪机\trtsp://admin:A12345678@192.168.2.3:554/11/1\n" +
                "厂房左前门枪机\trtsp://admin:A12345678@192.168.2.3:554/12/1\n" +
                "监控室外枪机\trtsp://admin:A12345678@192.168.2.3:554/13/1\n" +
                "二阶段2设备区枪机\trtsp://admin:A12345678@192.168.2.3:554/14/1\n" +
                "二阶段1球机\trtsp://admin:A12345678@192.168.2.3:554/15/1\n" +
                "二阶段1设备区枪机\trtsp://admin:A12345678@192.168.2.3:554/16/1\n" +
                "一阶段球机\trtsp://admin:A12345678@192.168.2.3:554/17/1\n" +
                "调水池区域枪机\trtsp://admin:A12345678@192.168.2.3:554/18/1\n" +
                "二阶段3设备区枪机\trtsp://admin:A12345678@192.168.2.3:554/19/1\n" +
                "厂房左后门枪机\trtsp://admin:A12345678@192.168.2.3:554/20/1\n" +
        "4-1鱼池\trtsp://admin:a1234567@192.168.2.122:554/cam/realmonitor?channel=21&subtype=1";

        // http://192.168.2.107:8089/rtsp/api/pull?target=rtsp%3A%2F%2Fadmin%3AA12345678%40192.168.2.3%3A554%2F1%2F1&streamPath=wh%2F%E4%BA%8C%E9%98%B6%E6%AE%B52%E7%90%83%E6%9C%BA%2F1&save=0

        String baseUrl = "http://192.168.2.107:8089/hdl/wh/";
        String[] cameraLines = camerasJsonStr.split("\n");

        try (BufferedWriter httpWriter = new BufferedWriter(new FileWriter("output.http"));
             BufferedWriter sqlWriter = new BufferedWriter(new FileWriter("updata.sql"))) {

            for (String line : cameraLines) {
                String[] parts = line.split("\t");
                if (parts.length == 2) {
                    String cameraName = parts[0];
                    String rtspUrl = parts[1];
                    try {
                        String encodedCameraName = URLEncoder.encode(cameraName, "UTF-8");
                        // 提取摄像头的编号
                        String cameraNumber = rtspUrl.split("/")[3];
                        String finalUrl = baseUrl + encodedCameraName + "/" + cameraNumber + ".flv";

                        // 写入.http文件
                        String httpFinalUrl = "### " + cameraName + " ###\nGET " + "http://192.168.2.107:8089/rtsp/api/pull?target=" + URLEncoder.encode(rtspUrl, "UTF-8") + "&streamPath=wh/" + encodedCameraName + "/" + cameraNumber + "&save=0";
                        httpWriter.write(httpFinalUrl);
                        httpWriter.newLine();

                        // 写入update.sql文件
                        String sqlUpdate = "UPDATE sd_video_config SET VIDEO_URL = '" + finalUrl + "' WHERE VIDEO_NAME = '" + cameraName + "';";
                        sqlWriter.write(sqlUpdate);
                        sqlWriter.newLine();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
